package com.hzz.service.report;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzz.api.report.IReportEventService;
import com.hzz.dao.report.IReportEventDao;
import com.hzz.dto.report.ReportEventDto;
import com.hzz.dto.report.ReportEventFinishDto;
import com.hzz.dto.report.ReportEventSupervisionDto;
import com.hzz.dto.report.ReportEventTreatmentDto;
import com.hzz.dto.user.UserDto;
import com.hzz.entity.report.ReportEvent;
import com.hzz.framework.Response;
import com.hzz.framework.SysErrorCode;
import com.hzz.framework.annotation.FeignService;
import com.hzz.framework.exception.ServiceException;
import com.hzz.framework.service.BaseService;
import com.hzz.framework.util.CommonConstant;
import com.hzz.framework.util.IdUtil;
import com.hzz.framework.util.JwtUtil;
import com.hzz.service.dsync.PatrolTaskDSync;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@FeignService
@Slf4j
public class ReportEventService extends BaseService implements IReportEventService {
    @Autowired
    IReportEventDao reportEventDao;
    @Autowired
    PatrolTaskDSync patrolTaskDSync;

    @Override
    public String deleteData(ReportEventDto dto) throws Exception {
        String result = "success";
        try {
            ReportEvent entity = copyTo(dto, ReportEvent.class);
            if (reportEventDao.deleteByPrimaryKey(entity) == 0) {
                throw new RuntimeException("数据不存在!");
            }
        } catch (Exception e) {
            log.error("物理删除异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = CommonConstant.DB_DEFAULT_TIMEOUT, rollbackFor = {Exception.class, RuntimeException.class})
    public String deleteDataById(ReportEventDto dto) throws Exception {
        String result = "success";
        try {
            ReportEvent entity = copyTo(dto, ReportEvent.class);
            if (reportEventDao.deleteById(entity) == 0) {
                throw new RuntimeException("数据不存在!");
            }
        } catch (Exception e) {
            log.error("逻辑删除异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }

    @Override
    public PageInfo findDataIsPage(ReportEventDto dto) throws Exception {
        PageInfo pageInfo = null;
        try {
            ReportEvent entity = copyTo(dto, ReportEvent.class);
            if (entity.getReportingUserId() != null) {
                if (entity.getDutyUserId() == null) {
                    entity.setDutyUserId(entity.getReportingUserId());
                }
                if (entity.getTreatmentUserId() == null) {
                    entity.setTreatmentUserId(entity.getReportingUserId());
                }
            }
            PageHelper.startPage(PN(dto.getPageNum()), PS(dto.getPageSize()));
            List list = reportEventDao.findDataIsPage(entity);
            pageInfo = new PageInfo(list);
            pageInfo.setList(copyTo(pageInfo.getList(), ReportEventDto.class));
        } catch (Exception e) {
            log.error("信息[分页]查询异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return pageInfo;
    }

    @Override
    public List<ReportEventDto> findDataIsList(ReportEventDto dto) throws Exception {
        List<ReportEventDto> results = null;
        try {
            ReportEvent entity = copyTo(dto, ReportEvent.class);
            if (entity.getReportingUserId() != null) {
                if (entity.getDutyUserId() == null) {
                    entity.setDutyUserId(entity.getReportingUserId());
                }
                if (entity.getTreatmentUserId() == null) {
                    entity.setTreatmentUserId(entity.getReportingUserId());
                }
            }
            results = copyTo(reportEventDao.findDataIsList(entity), ReportEventDto.class);
        } catch (Exception e) {
            log.error("信息[列表]查询异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return results;
    }

    @Override
    public ReportEventDto findDataById(ReportEventDto dto) throws Exception {
        ReportEventDto result = null;
        try {
            ReportEvent entity = copyTo(dto, ReportEvent.class);
            result = copyTo(reportEventDao.selectByPrimaryKey(entity), ReportEventDto.class);
        } catch (Exception e) {
            log.error("信息[详情]查询异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = CommonConstant.DB_DEFAULT_TIMEOUT, rollbackFor = {Exception.class, RuntimeException.class})
    public String follow(ReportEventDto dto) throws Exception {
        String result = "success";
        try {
            ReportEvent entity = copyTo(dto, ReportEvent.class);
            reportEventDao.follow(entity);
        } catch (Exception e) {
            log.error("时间关注异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }

    @Override
    public Response newEvent(ReportEventDto dto) throws Exception {
        Response result = new Response(0, "success");
        try {
            ReportEvent entity = copyTo(dto, ReportEvent.class);
            //判断数据是否存在
            entity.setReportingDate(new Date());
            //新增
            if (entity.getId() == null) {
                entity.setId(IdUtil.nextId());
            }
            reportEventDao.insert(entity);
            result.data = entity.getId();
            // 同步
            // patrolTaskDSync.patrolTaskEvent(entity);
        } catch (Exception e) {
            log.error("事件上报异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }

    @Override
    public Response supervision(ReportEventSupervisionDto dto) throws Exception {
        Response result = new Response(0, "success");
        try {
            ReportEvent entity = copyTo(dto, ReportEvent.class);
            UserDto userDto = JwtUtil.getSubject(UserDto.class);
            entity.setCreateId(userDto.getId());
            //判断数据是否存在
            if (reportEventDao.isDataExist(entity) != 0) {
                //数据存在
                reportEventDao.update(entity);
                // 同步
                // patrolTaskDSync.patrolTaskEvent(entity);
            }
        } catch (Exception e) {
            log.error("事件督办异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }

    @Override
    public Response treatment(ReportEventTreatmentDto dto) throws Exception {
        Response result = new Response(0, "success");
        try {
            ReportEvent entity = copyTo(dto, ReportEvent.class);
            UserDto userDto = JwtUtil.getSubject(UserDto.class);
            entity.setCreateId(userDto.getId());
            //判断数据是否存在
            if (reportEventDao.isDataExist(entity) != 0) {
                //数据存在
                reportEventDao.update(entity);
                // 同步
                // patrolTaskDSync.patrolTaskEvent(entity);
            }
        } catch (Exception e) {
            log.error("事件处理异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }

    @Override
    public Response finish(ReportEventFinishDto dto) throws Exception {
        Response result = new Response(0, "success");
        try {
            ReportEvent entity = copyTo(dto, ReportEvent.class);
            UserDto userDto = JwtUtil.getSubject(UserDto.class);
            entity.setCreateId(userDto.getId());
            //判断数据是否存在
            if (reportEventDao.isDataExist(entity) != 0) {
                //数据存在
                reportEventDao.update(entity);
                // 同步
                 patrolTaskDSync.patrolTaskEvent(entity);
            }
        } catch (Exception e) {
            log.error("事件完结异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }
}
